package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.HotelFavorite;
import com.travel.entity.Scenic;
import com.travel.entity.ScenicFavorite;
import com.travel.entity.User;
import com.travel.service.HotelFavoriteService;
import com.travel.service.ScenicFavoriteService;
import com.travel.service.impl.HotelFavoriteServiceImpl;
import com.travel.service.impl.ScenicFavoriteServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class galleryServlet
 */
@WebServlet("/galleryServlet")
public class galleryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HotelFavoriteService hotelFavoriteService = new HotelFavoriteServiceImpl();
	private ScenicFavoriteService scenicFavoriteService = new ScenicFavoriteServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public galleryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = request.getParameter("op");
		if ("HotelFavorite".equals(op)) {
			// 查询列表
			doQueryHotelFavorite(request, response);

		} else if ("ScenicFavorite".equals(op)) {
			doQueryScenicFavorite(request, response);
		} else if ("delete".equals(op)) {
			doDeleteHF(request, response);
		} else if ("deleteScenic".equals(op)) {
			doDeleteSF(request,response);
		}
	}

	private void doDeleteSF(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String favoriteId = request.getParameter("favoriteId");
		scenicFavoriteService.deleteById(Integer.parseInt(favoriteId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int userId = Integer.parseInt(request.getParameter("userid"));
		PageInfo<ScenicFavorite> scenicFavorite = scenicFavoriteService.getScenicFavoriteByPage(pageNum, pageSize,
				userId);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(scenicFavorite);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doDeleteHF(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String favoriteId = request.getParameter("favoriteId");
		hotelFavoriteService.deleteById(Integer.parseInt(favoriteId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int userId = Integer.parseInt(request.getParameter("userid"));
		PageInfo<HotelFavorite> hotelFavorites = hotelFavoriteService.getUserListByPage(pageNum, pageSize, userId);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(hotelFavorites);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);

	}

	private void doQueryScenicFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int userId = Integer.parseInt(request.getParameter("userid"));
		PageInfo<ScenicFavorite> scenicFavorite = scenicFavoriteService.getScenicFavoriteByPage(pageNum, pageSize,
				userId);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(scenicFavorite);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doQueryHotelFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));

		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int userId = Integer.parseInt(request.getParameter("userid"));
		PageInfo<HotelFavorite> hotelFavorites = hotelFavoriteService.getUserListByPage(pageNum, pageSize, userId);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(hotelFavorites);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
		
	}

}
